/*
 * "$Id: testcups.c 11205 2013-07-31 18:06:15Z msweet $"
 *
 *   CUPS API test program for CUPS.
 *
 *   Copyright 2007-2013 by Apple Inc.
 *   Copyright 2007 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Apple Inc. and are protected by Federal copyright
 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 *   which should have been included with this file.  If this file is
 *   file is missing or damaged, see the license at "http://www.cups.org/".
 *
 *   This file is subject to the Apple OS-Developed Software exception.
 *
 * Contents:
 *
 *   main()        - Main entry.
 *   dests_equal() - Determine whether two destinations are equal.
 */

/*
 * Include necessary headers...
 */

#undef _CUPS_NO_DEPRECATED
#include "cups/cups.h"
#include <stdlib.h>
#include <stdio.h> 
#include "sys_log.h"

#if 0
int					/* O - Exit status */
main(int  argc,				/* I - Number of command-line arguments */
     char *argv[])			/* I - Command-line arguments */
{
	int		status = 0,		/* Exit status */
		i,			/* Looping var */
		num_dests;		/* Number of destinations */
	cups_dest_t	*dests,			/* Destinations */
		*dest,			/* Current destination */
		*named_dest;		/* Current named destination */
	int		num_jobs;		/* Number of jobs for queue */
	cups_job_t	*jobs;			/* Jobs for queue */
	cups_job_t	*tmp_jobs;			/* Jobs for queue */
	cups_option_t *option;



	cups_ptype_t	mask = CUPS_PRINTER_LOCAL,
			/* Printer type mask */
	type = CUPS_PRINTER_LOCAL;
			/* Printer type */
	int		msec = 3000;	/* Timeout in milliseconds */


	/*get dests by type*/		

  num_dests = cupsGetDests(&dests);

  if (num_dests == 0)
  {
    SYS_LOG("GetDests FAIL\n");
    return (1);
  }
  else
  {
    SYS_LOG("PASS (%d dests)\n", num_dests);

    for (i = num_dests, dest = dests; i > 0; i --, dest ++)
    {
      SYS_LOG(" dest->name  [%s]\n", dest->name);

      if (dest->instance)
        SYS_LOG(" dest->instance [%s]\n", dest->instance);

      if (dest->is_default)
        SYS_LOG(" ***DEFAULT***\n");


	  
    }
  }

  dest = dests;

  SYS_LOG("*****************************************************************\n");
  SYS_LOG(" ***[%s]num_options[%d]***\n", dest->name, dest->num_options);
  for (i = 0, option = dest->options; i < dest->num_options; i ++, option ++)
  {
        SYS_LOG(" option name[%s] value[%s]\n", option->name,option->value);
	  
  }
  SYS_LOG("*****************************************************************\n");

  if (cupsPrintFile(dest->name, "./pixmaps/input.jpeg", "Test Page",
                    dest->num_options, dest->options) <= 0)
  {
    SYS_LOG("Print FAIL (%s)\n\n", cupsLastErrorString());
    return (1);
  }
  else
    SYS_LOG("Print PASS\n\n");

 /*
  * cupsGetJobs()
  */
#if 1
  SYS_LOG(" ***[%s]num_options[%d]***\n",dest->name,dest->num_options);
  for (i = 0, option = dest->options; i < dest->num_options; i ++, option ++)
  {
        SYS_LOG(" option name[%s] value[%s]\n", option->name,option->value);
	  
  }
  SYS_LOG("*****************************************************************\n");

#endif

 
  num_jobs = cupsGetJobs(&jobs, NULL, 0, -1);
  if (num_jobs == 0)
  {
    SYS_LOG("get jobs FAIL\n");
  }
  else
    SYS_LOG("cupsGetJobs PASS\n");
  tmp_jobs = jobs;
  for (i = 0; i < num_jobs; i ++)
  {
      SYS_LOG(" jobs id[%d] name[%s] value[%d] format[%s]\n", tmp_jobs->id,tmp_jobs->title,tmp_jobs->state,tmp_jobs->format);
	  tmp_jobs++;
  }
 
  //cupsFreeJobs(num_jobs, jobs);
  //cupsFreeDests(num_dests, dests);
	
	sleep(5);
  return (status);
}


cupsAddOption(const char    *name,	/* I  - Name of option */
              const char    *value,	/* I  - Value of option */
	      int           num_options,/* I  - Number of options */
              cups_option_t **options)	/* IO - Pointer to options */

typedef enum /**** Printer States.... */ 
{ 
  IPP_PRINTER_IDLE = 3, 
  IPP_PRINTER_PROCESSING, 
  IPP_PRINTER_STOPPED 
} ipp_pstate_t; 


#endif
static int printer_index = 0;

static void printing_set_print_r230(cups_dest_t	*dest)
{
    if(!dest) return;

     printf("printing_set_print_r230 in  \n");



   //  const char	*ppd_name = cupsGetPPD(dest->name);

   
     //dest->num_options = cupsAddOption("media", CUPS_MEDIA_A6,dest->num_options,&(dest->options));
     printf("printing_set_print_r230 out  \n");

}



int printing_file(char* file_path)
{
	int		status = 1,		/* Exit status */
		num_dests,i;		/* Number of destinations */
	cups_dest_t	*dests,			/* Destinations */
		*dest,			/* Current destination */
		*named_dest;		/* Current named destination */

	cups_option_t *option;

	num_dests = cupsGetDests(&dests);
	if (num_dests == 0)	{	
		SYS_LOG("GetDests FAIL");	
		return -1;	
	}  
    dest = dests;
    dest = dest+printer_index;

	if (cupsPrintFile(dest->name, file_path, "Test Page", dest->num_options, dest->options) <= 0)
	{
		SYS_LOG("Print FAIL (%s)\n\n", cupsLastErrorString());
		return (-1);
	}
	else
		SYS_LOG("\n Print PASS [%s] \n",file_path);

	cupsFreeDests(num_dests, dests);
//	remove(file_path);
	//sleep(5);
	return (status);
}

int printing_get_status()
{
	  int	  i=0, num_dests,j;	  
	  cups_dest_t *dests, *dest,*named_dest;		  
	  cups_option_t *option;
	  int		num_jobs;
	  cups_job_t  *jobs;		 
	  cups_job_t  *tmp_jobs;		  
	  int	print_status = 1;


      //set print
      SYS_LOG("\n\n\n !!!!!!!!!!!!!printing_get_status!!!!!!!!!!!  \n");
      printer_index = 0;
	  num_dests = cupsGetDests(&dests);
	  if (num_dests == 0) {   
		  SYS_LOG("GetDests FAIL");  
		  return -1;  
	  } else {
		//  SYS_LOG("PASS (%d dests)\n", num_dests);
		  printf("num_dests=%d\n\n",num_dests);
    	  for (i = 0, dest = dests; i < num_dests; i ++, dest ++)
    	  {

        	for (j = 0, option = dest->options; j < dest->num_options; j ++, option ++)
        	{

                 if(0 == strcmp(option->name,"printer-state") && (0 == strcmp(option->value,"3") || 0 == strcmp(option->value,"4"))) {
                    printing_set_print_r230(dest);
                    printer_index = i;
                 }else{
     
                 }
        	}
    	  }
	  }

	num_jobs = cupsGetJobs(&jobs, NULL, 0, -1);
    if (num_jobs == 0) {   
        SYS_LOG("GetDests FAIL\n");  
        return 1; 
    }

	tmp_jobs = jobs;
	for (i = 0; i < num_jobs; i ++)
	{
#if 0
	  IPP_JSTATE_PENDING = 3,		/* Job is waiting to be printed */
	  IPP_JSTATE_HELD,			/* Job is held for printing */
	  IPP_JSTATE_PROCESSING,		/* Job is currently printing */
	  IPP_JSTATE_STOPPED,			/* Job has been stopped */
	  IPP_JSTATE_CANCELED,			/* Job has been canceled */
	  IPP_JSTATE_ABORTED,			/* Job has aborted due to error */
	  IPP_JSTATE_COMPLETED			/* Job has completed successfully */
#endif
	  //	SYS_LOG(" jobs id[%d] name[%s] value[%d] format[%s]\n", tmp_jobs->id,tmp_jobs->title,tmp_jobs->state,tmp_jobs->format);
		//printf(" jobs id[%d] state[%d] name[%s] value[%d] format[%s]\n", tmp_jobs->id,tmp_jobs->state,tmp_jobs->title,tmp_jobs->state,tmp_jobs->format);
		if(tmp_jobs->state == 3 || tmp_jobs->state == 4 || tmp_jobs->state == 8) {
		    print_status = -2;
		}
	  	tmp_jobs++;
	}
	 
	cupsFreeJobs(num_jobs, jobs);
	cupsFreeDests(num_dests, dests);
	SYS_LOG("printing_get_status out\n");  
	return print_status;
}

int printing_cancle_jobs()
{
	int	  i=0, num_dests;	  
	cups_dest_t *dests, *dest,*named_dest;		  
	cups_option_t *option;
	int		num_jobs;
	cups_job_t  *jobs;		 
	cups_job_t  *tmp_jobs;		  
	int	print_status = 1;

	num_dests = cupsGetDests(&dests);
	if (num_dests == 0) {   
	  SYS_LOG("GetDests FAIL");  return -1;  
	} 

	dest = dests;
	num_jobs = cupsGetJobs(&jobs, NULL, 0, -1);
	tmp_jobs = jobs;
	for (i = 0; i < num_jobs; i ++)
	{
#if 0
		  IPP_JSTATE_PENDING = 3,		/* Job is waiting to be printed */
		  IPP_JSTATE_HELD,			/* Job is held for printing */
		  IPP_JSTATE_PROCESSING,		/* Job is currently printing */
		  IPP_JSTATE_STOPPED,			/* Job has been stopped */
		  IPP_JSTATE_CANCELED,			/* Job has been canceled */
		  IPP_JSTATE_ABORTED,			/* Job has aborted due to error */
		  IPP_JSTATE_COMPLETED			/* Job has completed successfully */
#endif
		SYS_LOG(" jobs id[%d] name[%s] value[%d] format[%s]\n", tmp_jobs->id,tmp_jobs->title,tmp_jobs->state,tmp_jobs->format);
		if(tmp_jobs->state == 3 || tmp_jobs->state ==4 || tmp_jobs->state ==5 || tmp_jobs->state ==6  || tmp_jobs->state ==8 ) {
			 cupsCancelJob(dest, tmp_jobs->id);
		}
		tmp_jobs++;
	}
	 
	cupsFreeJobs(num_jobs, jobs);
	cupsFreeDests(num_dests, dests);
	return print_status;

}

